from django.contrib.sessions import serializers
from django.core.serializers import json
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
# Create your views here.
from django.views.decorators.http import require_http_methods

from myapp.models import Book

@require_http_methods(["GET"])
def add_book(request):
    response = {}
    try:
        book = Book(book_name=request.GET.get('book_name'))
        book.save()
        response['msg'] = 'success'
        response['error_num'] = 0
    except:
        response['msg'] = str('wrong')
        response['error_num'] = 1

    return JsonResponse(response)

@require_http_methods(["GET"])
def show_books(request):
    response = {}
    try:
        books = Book.objects.filter()
        # response['list']  = books
        response['msg'] = 'success'
        response['error_num'] = 0
    except:
        response['msg'] = str('wrong')
        response['error_num'] = 1

    return JsonResponse(response)